﻿package learnmath.windows.elements{
/*-------------------------------------------------------------
	Created by: Ionel Alexandru 
	Mail: ionel.alexandru@gmail.com
	Site: www.learn-math.info
---------------------------------------------------------------*/
import learnmath.windows.*;
import learnmath.windows.elements.buttons.*;
import learnmath.windows.menu.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.ui.*;
import fl.containers.*;


public class WindowXP extends Pannel{

	var id:String;

	protected var windowsManager:WindowsManager;
	var littleIcon:MovieClip;
	public var titleBar:MovieClip;
	var color1:MovieClip;
	var color2:MovieClip;
	
	public var name:String;
	private var tf:TextFormat;
	
	public var active:Boolean = true;
	public var status = "NORMAL";
	public var bMin;
	public var bClose;
	var bMax;
	var bRestore;
	protected var statusBox:StatusBox;
	

	public function WindowXP(_windowsManager:WindowsManager, parent:MovieClip, _littleIcon:MovieClip, _name:String, _id:String, x:int, y:int, _width:int, _height:int){
		super(parent, x, y, _width, _height);
		id = _id;
		windowsManager = _windowsManager;
		tf = ConfigManager.getTextFormatForTitle();
		littleIcon = _littleIcon;
		name = _name;
	}

	public function draw():void{
		trace("redraw " + name);
		pannel.graphics.clear();
		while(pannel.numChildren > 0) {
			pannel.removeChildAt(0);
		}
		
		drawMouseResize();
		
		pannel.graphics.lineStyle(1,0xd4cfc9);
		pannel.graphics.beginFill(0xd4cfc9);   
		pannel.graphics.drawRect(0, 0, width-1, height-1);   
		pannel.graphics.endFill();
		
		
		pannel.graphics.lineStyle(1,0xffffff);
		pannel.graphics.moveTo(0,0);
		pannel.graphics.lineTo(width, 0);
		pannel.graphics.moveTo(0,0);
		pannel.graphics.lineTo(0, height);
		
		pannel.graphics.lineStyle(1,0x433e3a);
		pannel.graphics.moveTo(width-1, height);
		pannel.graphics.lineTo(width-1, 0);
		pannel.graphics.moveTo(width, height-1);
		pannel.graphics.lineTo(0, height-1);
		
		pannel.graphics.lineStyle(1,0x847f7b);
		pannel.graphics.moveTo(width-2, height-1);
		pannel.graphics.lineTo(width-2, 1);
		pannel.graphics.moveTo(width-1, height-2);
		pannel.graphics.lineTo(1, height-2);
		
		titleBar = new MovieClip();
		titleBar.x = 3;
		titleBar.y = 3;
		pannel.addChild(titleBar);
		
		color1 = new MovieClip();
		color2 = new MovieClip();
		color1.mouseEnabled = false;
		color2.mouseEnabled = false;
		titleBar.addChild(color1);
		titleBar.addChild(color2);

		var colors:Array = [0x092469, 0xb7c7d7];
		var matr:Matrix = new Matrix();
		matr.createGradientBox(width-7, 18);
		
		color1.graphics.lineStyle();
		color1.graphics.beginGradientFill(GradientType.LINEAR, colors, [1, 1], [0, 255], matr, SpreadMethod.PAD);  
		color1.graphics.drawRect( 0, 0, width-7, 18);
		color1.graphics.endFill();

		colors = [0x858480, 0xc0bfbb];
		color2.graphics.lineStyle();
		color2.graphics.beginGradientFill(GradientType.LINEAR, colors, [1, 1], [0, 255], matr, SpreadMethod.PAD);  
		color2.graphics.drawRect( 0, 0, width-7, 18);
		color2.graphics.endFill();


		titleBar.addChild(littleIcon);
		littleIcon.mouseEnabled = false;
		littleIcon.x=1;
		littleIcon.y=1;
		
		var t:TextField = new TextField();
		t.defaultTextFormat = tf;
		t.text = name;
		t.selectable = false;
		t.x = 20;
		t.y = 0;
		t.mouseEnabled = false;
		titleBar.addChild(t);
		
		bClose = new ButtonCloseXP(titleBar, width-25, 2);
		
		bMax = new ButtonMaxXP(titleBar, width-43, 2);
		bMax.pannel.addEventListener(MouseEvent.MOUSE_UP, maximize);
		bMax.pannel.name="MaximizeButton";
		
		bRestore = new ButtonRestoreXP(titleBar, width-43, 2);
		bRestore.pannel.addEventListener(MouseEvent.MOUSE_UP, maximize);
		bRestore.pannel.name="MaximizeButton";
		
		if(this.status=="NORMAL"){
			this.bRestore.pannel.visible=false;
			this.bMax.pannel.visible=true;
		}else{
			this.bRestore.pannel.visible=true;
			this.bMax.pannel.visible=false;
		}
		bMin = new ButtonMinXP(titleBar, width-59, 2);
		
		titleBar.buttonMode = true;
		titleBar.mouseEnabled = true;
		titleBar.doubleClickEnabled = true;
		titleBar.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
		titleBar.addEventListener(MouseEvent.MOUSE_UP, dropIt);
		titleBar.addEventListener(MouseEvent.DOUBLE_CLICK, maximize);
		titleBar.name="TitleBar";
		
		redrawWorkingPannel();

		var tfs = ConfigManager.getTextFormatForStatus();
		statusBox = new StatusBox(pannel, 3, height-21, width-8, 18, "", tfs);
		redrawStatusBar(statusBox);
		
		windowsManager.addListenerToButtons(this);
		activateWindow();
		
	}


	public function redrawWorkingPannel():void {
	}

	public function redrawStatusBar(_statusBox:StatusBox):void {
	}

	public function drawOnFrame():void{
	}
	
	public function setActivateWindow(_active:Boolean):void {
		active = _active;
		activateWindow();
	}
	
	function activateWindow():void {
		if(active){
			color2.visible = false;
			color1.visible = true;
		}else{
			color1.visible = false;
			color2.visible = true;
		}

	}

	function drawMouseResize():void {
		var leftUpper = new MovieClip();
		leftUpper.graphics.lineStyle(1,0xd4cfc9);
		leftUpper.graphics.beginFill(0xd4cfc9);   
		leftUpper.graphics.drawRect(0, 0, 20, 20);   
		leftUpper.graphics.endFill();
		leftUpper.addEventListener(MouseEvent.MOUSE_OVER, changeToNW);
		leftUpper.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		leftUpper.alpha = 0.1;
		pannel.addChild(leftUpper);

		var rightUpper = new MovieClip();
		rightUpper.graphics.lineStyle(1,0xd4cfc9);
		rightUpper.graphics.beginFill(0xd4cfc9);   
		rightUpper.graphics.drawRect(width-20, 0, 20, 20);   
		rightUpper.graphics.endFill();
		rightUpper.addEventListener(MouseEvent.MOUSE_OVER, changeToNE);
		rightUpper.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		rightUpper.alpha = 0.1;
		pannel.addChild(rightUpper);

		var rightDown1 = new MovieClip();
		rightDown1.graphics.lineStyle(1,0xd4cfc9);
		rightDown1.graphics.beginFill(0xd4cfc9);   
		rightDown1.graphics.drawRect(width-3, height-20, 3, 20);   
		rightDown1.graphics.endFill();
		rightDown1.addEventListener(MouseEvent.MOUSE_OVER, changeToSE);
		rightDown1.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		rightDown1.alpha = 0.1;
		pannel.addChild(rightDown1);
		var rightDown2 = new MovieClip();
		rightDown2.graphics.lineStyle(1,0xd4cfc9);
		rightDown2.graphics.beginFill(0xd4cfc9);   
		rightDown2.graphics.drawRect(width-20, height-3, 20, 3);   
		rightDown2.graphics.endFill();
		rightDown2.addEventListener(MouseEvent.MOUSE_OVER, changeToSE);
		rightDown2.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		rightDown2.alpha = 0.1;
		pannel.addChild(rightDown2);

		var leftDown1 = new MovieClip();
		leftDown1.graphics.lineStyle(1,0xd4cfc9);
		leftDown1.graphics.beginFill(0xd4cfc9);   
		leftDown1.graphics.drawRect(0, height-20, 3, 20);   
		leftDown1.graphics.endFill();
		leftDown1.addEventListener(MouseEvent.MOUSE_OVER, changeToSW);
		leftDown1.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		leftDown1.alpha = 0.1;
		pannel.addChild(leftDown1);
		
		var leftDown2 = new MovieClip();
		leftDown2.graphics.lineStyle(1,0xd4cfc9);
		leftDown2.graphics.beginFill(0xd4cfc9);   
		leftDown2.graphics.drawRect(0, height-3, 20, 3);   
		leftDown2.graphics.endFill();
		leftDown2.addEventListener(MouseEvent.MOUSE_OVER, changeToSW);
		leftDown2.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		leftDown2.alpha = 0.1;
		pannel.addChild(leftDown2);
		
		var up = new MovieClip();
		up.graphics.lineStyle(1,0xd4cfc9);
		up.graphics.beginFill(0xd4cfc9);   
		up.graphics.drawRect(20, 0, width-40, 3);   
		up.graphics.endFill();
		up.addEventListener(MouseEvent.MOUSE_OVER, changeToN);
		up.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		up.alpha = 0.1;
		pannel.addChild(up);
		
		var down = new MovieClip();
		down.graphics.lineStyle(1,0xd4cfc9);
		down.graphics.beginFill(0xd4cfc9);   
		down.graphics.drawRect(20, height-3, width-40, 3);   
		down.graphics.endFill();
		down.addEventListener(MouseEvent.MOUSE_OVER, changeToS);
		down.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		down.alpha = 0.1;
		pannel.addChild(down);

		var left = new MovieClip();
		left.graphics.lineStyle(1,0xd4cfc9);
		left.graphics.beginFill(0xd4cfc9);   
		left.graphics.drawRect(0, 20, 3, height-40);   
		left.graphics.endFill();
		left.addEventListener(MouseEvent.MOUSE_OVER, changeToW);
		left.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		left.alpha = 0.1;
		pannel.addChild(left);

		var right = new MovieClip();
		right.graphics.lineStyle(1,0xd4cfc9);
		right.graphics.beginFill(0xd4cfc9);   
		right.graphics.drawRect(width-3, 20, 3, height-40);   
		right.graphics.endFill();
		right.addEventListener(MouseEvent.MOUSE_OVER, changeToE);
		right.addEventListener(MouseEvent.MOUSE_OUT, changeToNormal);
		right.alpha = 0.1;
		pannel.addChild(right);
	}

	function changeToNW(event:MouseEvent):void {
		windowsManager.setMouse("nwresize", event, pannel.appId);
	}
	function changeToNE(event:MouseEvent):void {
		windowsManager.setMouse("neresize", event, pannel.appId);
	}
	function changeToSE(event:MouseEvent):void {
		windowsManager.setMouse("seresize", event, pannel.appId);
	}
	function changeToSW(event:MouseEvent):void {
		windowsManager.setMouse("swresize", event, pannel.appId);
	}
	function changeToN(event:MouseEvent):void {
		windowsManager.setMouse("nresize", event, pannel.appId);
	}
	function changeToS(event:MouseEvent):void {
		windowsManager.setMouse("sresize", event, pannel.appId);
	}
	function changeToW(event:MouseEvent):void {
		windowsManager.setMouse("wresize", event, pannel.appId);
	}
	function changeToE(event:MouseEvent):void {
		windowsManager.setMouse("eresize", event, pannel.appId);
	}

	function changeToNormal(event:MouseEvent):void {
		windowsManager.resetMouse();
	}

	function pickUp(event:MouseEvent):void {
		if(!active) return;
		if(this.status!="MAXIMIZE"){
			if(event.target.name=="TitleBar"){
				event.target.parent.startDrag();
			}
		}
		//windowsManager.activateWindow(event);
	}
	function dropIt(event:MouseEvent):void {
		//if(event.target.name=="TitleBar"){
			event.target.parent.stopDrag();
		//}
	}

	function maximize(event:MouseEvent):void {
		if(event.target.name!="TitleBar" && event.target.name!="MaximizeButton") return;
		
		if(this.status=="MAXIMIZE"){
			this.pannel.x=this.pannel.oldX;
			this.pannel.y=this.pannel.oldY;
			this.width=this.pannel.oldWidth;
			this.height=this.pannel.oldHeight;
			this.status = "NORMAL";
		}else{
			this.pannel.oldX = this.pannel.x;
			this.pannel.oldY = this.pannel.y;
			this.pannel.x=0;
			this.pannel.y=0;
			this.pannel.oldWidth = this.width;
			this.pannel.oldHeight = this.height;
			this.width=AppManager.mainPannel.width;
			this.height=AppManager.mainPannel.height-30;
			this.status = "MAXIMIZE";
		}
		windowsManager.activateWindow(event);
		this.draw();
	}
	
	public function executeMenu(event:MenuCustomEvent):void {
		if(event.menuId=="menu_file_close"){
			this.windowsManager.closeWindowId(this.pannel.appId);
		}else if(event.menuId=="menu_help_comm"){
			this.windowsManager.createAplication("Help-MathML-Editor");
		}else if(event.menuId=="menu_help_mathml"){
			this.windowsManager.createAplication("Help-MathML");
		}
	}
	
	
	public function processKey(event:KeyboardEvent):void {
	}	

}
}